package cn.deali.homework9.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.deali.homework9.R;
import cn.deali.homework9.activity.OnlineMusicAlbumActivity;
import cn.deali.homework9.model.TopMusicList;

public class TopMusicListAdapter extends RecyclerView.Adapter {
    Context context;
    List<TopMusicList> topMusicLists;

    public TopMusicListAdapter(Context context, List<TopMusicList> topMusicLists) {
        this.context = context;
        this.topMusicLists = topMusicLists;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.top_list_item, null);

        return new TopMusicListAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final TopMusicList topMusicList = topMusicLists.get(position);
        final ViewHolder viewHolder = (ViewHolder) holder;

        if (topMusicList.getMusics().size() >= 3) {
            viewHolder.song_name1.setText("1." + topMusicList.getMusics().get(0).getName());
            viewHolder.song_name2.setText("2." + topMusicList.getMusics().get(1).getName());
            viewHolder.song_name3.setText("3." + topMusicList.getMusics().get(2).getName());
        }

        Picasso.get().load(topMusicList.getCoverUrl()).into(viewHolder.imageView);

        //点击进入某种音乐列表
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), OnlineMusicAlbumActivity.class);
                intent.putExtra("idx", topMusicList.getIdx());
                intent.putExtra("name", topMusicList.getName());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return topMusicLists.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView song_name1;
        TextView song_name2;
        TextView song_name3;


        ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img);
            song_name1 = itemView.findViewById(R.id.song_name1);
            song_name2 = itemView.findViewById(R.id.song_name2);
            song_name3 = itemView.findViewById(R.id.song_name3);
        }
    }
}
